<?php
/*
 * 手机客户端 秀场更多作品接口
 * 传入参数 
 * 2013-12-011
 */
include "inc/public_header.php";
include "inc/MiniaturePro.class.php";
include "func/user.php";				//用户信息处理函数
include "func/pic.php";					//图片处理函数

//头像根路径
$LOGO_BASE_PATH = "http://www.chinalaohu.com/modules/miniature.php?w=150&h=150&p=/uploads/zjl_ilogo/";

//默认头像
$LOGO_DEFAULT_PATH = "http://www.chinalaohu.com/images/zjl/logo.png";

//获取用户信息
$uInfoArr = user_getInfoFromUUID();
if($uInfoArr){
	$uid = $uInfoArr["uid"];				//用户信息主键
}else{
	$uid = "";
}



//页数，第一页为0
$page			= $_REQUEST["page"]; 
//每页记录数			
$pagesize		= $_REQUEST["pagesize"];

//构建返回值
$reJson = array();

$worksid	    = $_REQUEST["worksId"];			//作品id
$zpuid 			= $_REQUEST["authorId"];		//作者id
$hd_id			= $_REQUEST["hd_id"];			//活动主键

if(!isset($page)){
	$page = 0;
}
if(!isset($pagesize)){
	$pagesize = 10; //默认每页10条
}

//获取作者id
if(!$zpuid){
	$sql ="select uid from new_weituo_zuopin where id='".$worksid."'";
	$db->query($sql);
	$db->next_record();
	$zpuid = $db->f(uid);
}
$reJson["userId"] = $zpuid;

//设置是否关注过这个人  0否 1是
if($uid==""){
	$reJson["guanzhu"] = "0";
}else{
	$sql = "select count(0) as ct from new_weituo_fensi where fuid='".$uid."' and uid='".$zpuid."'";
	if(isset($hd_id) && $hd_id!=""){
		$sql = $sql." and hd_id='".$hd_id."' ";
	}
	$db->query($sql);
	$db->next_record();
	$igz = false;
	if($db->f("ct")>0){
		$igz = true;
	}
	
	$sql = "select count(0) as ct from new_weituo_fensi where uid='".$uid."' and fuid='".$zpuid."'";
	if(isset($hd_id) && $hd_id!=""){
		$sql = $sql." and hd_id='".$hd_id."' ";
	}
	$db->query($sql);
	$db->next_record();
	$ogz = false;
	if($db->f("ct")>0){
		$ogz = true;
	}
	if($igz && $ogz){
		//相互关注
		$reJson["guanzhu"] = "3";
	}else if ($igz){
		//我关注了对方
		$reJson["guanzhu"] = "1";
	}else if($ogz){
		//对方关注了我
		$reJson["guanzhu"] = "2";
	}else{
		//谁也没搭理谁
		$reJson["guanzhu"] = "0";
	}
}

//作品总数
$sql="  select 
			count(0) as zp_ct
		from 
			new_weituo_zuopin a 
			INNER JOIN new_weituo_person b on a.uid=b.uid
		where
			a.uid='".$zpuid ."'
			and a.aflag='1'
			and a.dr='0'
";
if(isset($hd_id) && $hd_id!=""){
	$sql = $sql." and a.hd_id='".$hd_id."' ";
}
if ($hd_id==6){
	$sql = $sql." or (a.hd_id=1 and b.xuexiao_id=28) ";
}
$db->query($sql);
$db->next_record();
$reJson["count"] = $db->f("zp_ct");
	
//作者基本资料
$sql="  select
			a.nickname
			,a.ilogo_path
			,a.xuexiao
			,a.xuexiao_id
			,b.aname as xuexiao_id_name
			,c.mobile
		from 
			new_weituo_person a 
			inner join user_base c on a.uid=c.uid
			left join new_weituo_xuexiao b on a.xuexiao_id=b.id 
		where
			a.uid='".$zpuid ."'
";
$db->query($sql);
if ($db->next_record()){
	$nickName = $db->f("nickname");
	if($nickName==null){
		$nickName = user_fix_mobile($db->f("mobile"));
	}
	$userIcon = $db->f("ilogo_path");
	if($userIcon==null || $userIcon==""){
		$userIcon = $LOGO_DEFAULT_PATH;
	}else{
		$userIcon=$LOGO_BASE_PATH.$userIcon;
	}
	$reJson["success"] = "1";
	$reJson["userName"] = $nickName;	//作者昵称
	$reJson["userIcon"] = $userIcon;	//作者头像
	$reJson["userCollege"] = $db->f("xuexiao")?$db->f("xuexiao"):"";	//作者学校
}else{
	$reJson["userIcon"] = $LOGO_DEFAULT_PATH;
}

//作者关注（人）统计
$sql=" select count(0) as gz_ct from new_weituo_fensi where fuid='".$zpuid."'";
if(isset($hd_id) && $hd_id!=""){
	$sql = $sql." and hd_id='".$hd_id."' ";
}
$db->query($sql);
$db->next_record();
$reJson["attention"] = $db->f("gz_ct"); 

//作者粉丝统计
$sql=" select count(0) as gz_ct from new_weituo_fensi where uid='".$zpuid."'";
if(isset($hd_id) && $hd_id!=""){
	$sql = $sql." and hd_id='".$hd_id."' ";
}
$db->query($sql);
$db->next_record();
$reJson["fans"] = $db->f("gz_ct"); 

/*
//作者赞统计
$sql="select count(0) as zans_ct from new_weituo_pingfen where zan=1 and zpid in (select id from new_weituo_zuopin where uid='".$zpuid."')";
$db->query($sql);
$db->next_record();
$reJson["praise"] = $db->f("zans_ct"); 
*/

//作品总数统计
$sql="  SELECT 
			count(0) as zp_ct
		from 
			new_weituo_zuopin a 
			INNER JOIN new_weituo_person b on a.uid=b.uid
		where
			a.uid='".$zpuid ."'
			and a.aflag='1'
			and a.dr='0'
";
if(isset($hd_id) && $hd_id!=""){
	$sql = $sql." and a.hd_id='".$hd_id."' ";
}
if ($hd_id==6){
	$sql = $sql." or (a.hd_id=1 and b.xuexiao_id=28) ";
}
$db->query($sql);
$db->next_record();
$reJson["works"] = $db->f("zp_ct"); 


$imgWidth = '200';
//作品信息列表
$sql ="select 
				a.id
				,a.aname
				,a.czln
				,a.pingfen
				,a.img_path
				,a.img_id
				,f.ct as zan_count
				,a.aflag
				,g.ct as pl_count
				,e.id as mini_img_id
				,e.awidth as mini_img_width
				,e.aheight as mini_img_height
				,e.aname as mini_img_aname
				,h.ct as gz_count
		from 
				new_weituo_zuopin a	
				left join new_weituo_person d on a.uid=d.uid
				left join new_weituo_pic_size e on a.img_id=e.img_id and e.awidth='".$imgWidth."'
				left join (select count(0) as ct,zpid from new_weituo_pingfen where zan='1' group by zpid) f on a.id=f.zpid
				left join (select count(0) as ct,zpid from new_weituo_comment where dr='0' group by zpid) g on a.id=g.zpid
				left join (select count(0) as ct,zpid from new_weituo_gz group by zpid) h on a.id=h.zpid
		where
				a.dr='0'
				and a.uid='".$zpuid."'
				and a.aflag='1'
		";
if(isset($hd_id) && $hd_id!=""){
	$sql = $sql." and a.hd_id='".$hd_id."' ";
}
if ($hd_id==6){
	$sql = $sql." or (a.hd_id=1 and d.xuexiao_id=28) ";
}
$sql = $sql."GROUP BY id limit ".($page * $pagesize).",".$pagesize;
$db->query($sql);
$worksArr = array();
$miniature = new MiniaturePro();	//构建缩略图处理类

while($db->next_record()){
	//处理生成缩略图
	$imgInfos = pic_resize(
							 $miniature
							,$db->f("img_path")
							,$db->f("mini_img_aname")
							,$imgWidth
							,$db->f("img_id")
							,$db->f("mini_img_id")
							,$db->f("mini_img_width")
							,$db->f("mini_img_height"));
	$imgUrl = "";
	if($imgInfos["success"]!="0"){
		$imgUrl = $imgInfos["mini_img_url"];
	}
	if($db->f("czln")){
		$czln=$db->f("czln");
	}else{
		$czln="未填写";
	}
	$worksArr[] = array(
		"title"=>$db->f("aname")							//作品名称
		,"workId"=>$db->f("id")								//作品id
		,"imgUrl"=>$imgUrl				//图片
		,"level"=>$db->f("pingfen")							//评分
		,"praiseCount"=>$db->f("zan_count")?$db->f("zan_count"):"0"					//赞
		,"ideas"=>$czln										//作品理念
		,"state"=>$db->f("aflag")							//审核状态
		,"replyCount"=>$db->f("pl_count")?$db->f("pl_count"):"0"					//评论统计
		,"imgWidth"=>(string)$imgInfos["mini_img_width"]	//图片宽度
		,"imgHeight"=>(string)$imgInfos["mini_img_height"]	//图片高度
		,"worksCollection"=>$db->f("gz_count")?$db->f("gz_count"):"0"				//作品被收藏次数
	);
}
$reJson["list"]=$worksArr;
echo json_encode($reJson);
?>